<template>
  <div class="detail-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane
          label="登录账户设置"
          name="first"
        ><el-form ref="form" :model="form" :rules="rules" label-width="280px">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="form.username" style="width:30%" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" style="width:30%" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="saveRoleMsg">更新</el-button>
            <el-button @click="$router.back()">取消</el-button>
          </el-form-item>
        </el-form></el-tab-pane>
        <el-tab-pane label="个人详情" name="second"><info /></el-tab-pane>
        <el-tab-pane label="岗位信息" name="third"><Job /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { getMoreUserInfo } from '../../api/user'
import { saveUserDetailById } from '../../api/employees'
import info from './components/Info.vue'
import Job from './components/Jobs.vue'
export default {
  name: 'Detail',
  components: { info, Job },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码为6-9位', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getMoreUserInfo({ id: this.$route.params.id })
    this.userInfo = res
    this.form.username = res.username
  },
  methods: {
    saveRoleMsg() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          await saveUserDetailById({ ...this.userInfo, ...this.form })
          this.$message.success('更新成功')
          this.$router.back()
        } else {
          this.$message.error('更新失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.detail-container {
  padding: 20px;
  .el-form {
    margin-top: 20px;
    width: 100%;
  }
}
</style>
